package de.szut.tree;

import java.util.NoSuchElementException;

/**
 * Wrapper for a replaced value
 * @author Marc Huisinga
 *
 * @param <T> - Type of the replaced value
 */
public final class Replaced<T> {

	private final T value;
	private final boolean replaced;
	
	/**
	 * Constructor for a value that was replaced
	 * @param value - value that was replaced
	 */
	public Replaced(T value) {
		this.value = value;
		this.replaced = true;
	}
	
	/**
	 * Constructor for a value that was not replaced
	 */
	public Replaced() {
		this.value = null;
		this.replaced = false;
	}

	/**
	 * Gets the value that was replaced.
	 * Throws an NoSuchElementException if there was no value replaced.
	 * @return replaced value
	 */
	public T get() {
		if (!replaced) {
			throw new NoSuchElementException("Value was not replaced");
		}
		return value;
	}

	public boolean isReplaced() {
		return replaced;
	}
	
}
